package com.rui.study.algorithm.P_单链表反转;


import com.rui.study.algorithm.ListNode;

public class Solution_2 {

    public ListNode reverseList(ListNode head) {
        ListNode h = head;
        if (h != null) {
            ListNode p = h.next;
            h.next = null;
            ListNode n = null;
            if (p != null) {
                n = p.next;   // 暂存后继的后继节点
                while (p != null) {
                    p.next = h;
                    h = p;
                    p = n;
                    if (n != null) {
                        n = n.next;
                    }
                }
            }
        }
        return h;
    }
}
